package com.spelet.screens;

import java.io.File;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Files;
import com.badlogic.gdx.Files.FileType;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.TimeUtils;
import com.spelet.Main;
import com.spelet.Renderer;
import com.spelet.StaticFonts;
import com.spelet.StaticSprites;

public class CustomGame extends Screen{
	boolean available; // Fix code for if not available.
	public File currentDirectory;
	public File currentFile;
	public Sprite border;
	public Sprite marked;
	public Sprite up;
	public Sprite open;
	public Sprite exit;
	private long clickedAt;
	Rectangle bounds;
	int deltaY;
	int draggedX;
	int draggedY;
	Main main;
	public CustomGame (Main m) {
		main = m;
		clickedAt = 0;
		bounds = new Rectangle(0,0,0,0);
		border = new Sprite(new Texture(Gdx.files.internal("data/loadfile/border.png")));
		marked = new Sprite(new Texture(Gdx.files.internal("data/loadfile/marked.png")));
		up = new Sprite(new Texture(Gdx.files.internal("data/loadfile/up.png")));
		up.setPosition(300, Gdx.graphics.getHeight()-64);
		open = new Sprite(new Texture(Gdx.files.internal("data/loadfile/open.png")));
		open.setPosition(300,0);
		exit = StaticSprites.exit;
		if (Gdx.files.isExternalStorageAvailable()) {
			if (Gdx.files.external("GreenHatMan").exists() && Gdx.files.external("GreenHatMan").isDirectory()) {
				available = true;
				currentDirectory = Gdx.files.external("GreenHatMan").file();
			} else {
				Gdx.files.external("GreenHatMan").file().mkdir();
				available = true;
				currentDirectory = Gdx.files.external("GreenHatMan").file();
			}
		} else {
			available = false;
		}
	}
	@Override
	public boolean keyDown(int arg0) {
		if (Input.Keys.BACK == arg0) {
			main.setScreen(Main.SCREEN_MAINMENU);
		}
		return false;
	}

	@Override
	public boolean keyTyped(char arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean keyUp(int arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean scrolled(int arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDown(int x, int y, int arg2, int arg3) {
		y = Gdx.graphics.getHeight()-y;
		clickedAt = TimeUtils.millis();
		draggedX = x;
		draggedY = y;
		return false;
	}

	public boolean touchDragged(int x, int y, int arg2) {
		y= Gdx.graphics.getHeight()-y;
		deltaY += y-draggedY;
		if (deltaY < 0) {
			deltaY = 0;
		}
		draggedX = x;
		draggedY = y;
		return false;
	}

	@Override
	public boolean touchMoved(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean touchUp(int x, int y, int arg2, int arg3) {
		if (TimeUtils.millis()-clickedAt < 500) {
			y = Gdx.graphics.getHeight()-y;
			int i = 1;
			for (File f : currentDirectory.listFiles()) {
				if (f.isDirectory() || f.getName().endsWith(".GHM")) {
					bounds.set(20,Gdx.graphics.getHeight()-i*64+deltaY,256,64);
					if (bounds.contains(x,y)) {
						if (f.isDirectory()) {
							currentFile = f;
						} else {
							currentFile = f;
							System.out.println("CLICKED " + i);
						}
					}
					i++;
				}
			}
			exit.setPosition(open.getX()+open.getWidth(), open.getY());
			if (up.getBoundingRectangle().contains(x,y) && currentDirectory.getParentFile() != null) {
				currentDirectory = currentDirectory.getParentFile();
				currentFile = null;
			} else if (open.getBoundingRectangle().contains(x,y) && currentFile != null) {
				if (currentFile.isDirectory()) {
					currentDirectory = currentFile;
					currentFile = null;
					deltaY = 0;
				} else {
					((Game)main.getState(Main.SCREEN_GAME)).customLevel(
							Gdx.files.getFileHandle(currentFile.getPath(), FileType.Internal).readString());
					main.setScreen(Main.SCREEN_GAME);
				}
			} else if (exit.getBoundingRectangle().contains(x,y)) {
				main.setScreen(Main.SCREEN_MAINMENU);
			}
		}
		return false;
	}

	@Override
	public void update() {

	}

	public void render() {
		Renderer.getInstance().renderLoadFile(this);
	}
	public void render(SpriteBatch sb) {
		int i = 1;
		for (File f : currentDirectory.listFiles()) {
			if (f.isDirectory() || f.getName().endsWith(".GHM")) {
				if (f.equals(currentFile)) {
					marked.setPosition(20,Gdx.graphics.getHeight()-i*64+deltaY);
					marked.draw(sb);
				}
				border.setPosition(20,Gdx.graphics.getHeight()-i*64+deltaY);
				border.draw(sb);
				StaticFonts.basicFont.draw(sb, f.getName(), 40, Gdx.graphics.getHeight()-i*64+40+deltaY);
				i++;
			}
		}
		if (currentDirectory.getParent() == null) {
			up.draw(sb,0.7f);
		} else {
			up.draw(sb);	
		}
		if (currentFile == null) {
			open.draw(sb,0.7f);
		} else {
			open.draw(sb);
		}
		exit.setPosition(open.getX()+open.getWidth(), open.getY());
		exit.draw(sb);
	}

	@Override
	public void enter() {
		Gdx.input.setInputProcessor(this);
		Gdx.input.setCatchBackKey(true);
	}
	public void restartTest() {
		((Game)main.getState(Main.SCREEN_GAME)).customLevel(
				Gdx.files.getFileHandle(currentFile.getPath(), FileType.Internal).readString());
		main.setScreen(Main.SCREEN_GAME);

	}

}

